﻿using System.Web.Mvc;
using SDF.Core;
using SDF.Core.Caching;
using SDF.Services.Configuration;
using SDF.Services.Localization;
using SDF.Services.Media;
using SDF.Services.Projects;
using SDF.Web.Framework.Controllers;
using SDF.Services.Security;
using SDF.Services.Helpers;
using SDF.Core.Infrastructure;
using SDF.Plugin.Misc.SAPAdapter.Models;
using SDF.Plugin.Misc.SAPAdapter.Services;
using SDF.Core.Data;
using SDF.Plugin.Misc.SAPAdapter.Extensions;
using SDF.Web.Framework.Kendoui;
using System.Linq;
using SDF.Plugin.Misc.SAPAdapter.Domain;

namespace SDF.Plugin.Misc.SAPAdapter.Controllers
{
    [AdminAuthorize]
    public class SAPInstanceController : SAPControlBase
    {
        private readonly IWorkContext _workContext;
        private readonly IProjectContext _projectContext;
        private readonly IProjectService _projectService;
        private readonly ISettingService _settingService;
        private readonly ICacheManager _cacheManager;
        private readonly ILocalizationService _localizationService;
        private readonly IPermissionService _permissionService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly ISAPInstanceService _sapInstanceService;

        public SAPInstanceController(IWorkContext workContext,
            IProjectContext projectContext,
            IProjectService projectService,
            ISettingService settingService,
            ICacheManager cacheManager,
            ILocalizationService localizationService,
            IPermissionService permissionService,
            IDateTimeHelper dateTimeHelper,
            ISAPInstanceService sapInstanceService)
        {
            this._workContext = workContext;
            this._projectContext = projectContext;
            this._projectService = projectService;
            this._settingService = settingService;
            this._cacheManager = cacheManager;
            this._localizationService = localizationService;
            this._permissionService = permissionService;
            this._dateTimeHelper = dateTimeHelper;
            this._sapInstanceService = sapInstanceService;
        }

        public ActionResult Index()
        {
            return View("~/Plugins/SDF.Plugin.Misc.SAPAdapter/Views/SAPInstance/List.cshtml", new SAPClientSearchModel());
        }

        public ActionResult Query(SAPClientSearchModel model, DataSourceRequest command)
        {
            var sapClients = _sapInstanceService.QuerySAPInstance(model.SAPInstance, command.Page - 1, command.PageSize);

            var gridModel = new DataSourceResult
            {
                Data = sapClients.Select(x => { return x.ToModel(); })
                    .Select(p => new { Id = p.Id, CodeName = p.CodeName, Name = p.Name, ServerDisplay = p.ServerDisplay }),
                Total = sapClients.TotalCount
            };

            return new JsonResult
            {
                Data = gridModel
            };
        }

        public ActionResult Create()
        {
            return View("~/Plugins/SDF.Plugin.Misc.SAPAdapter/Views/SAPInstance/Create.cshtml", new SAPClientModel());
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public ActionResult Create(SAPClientModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                var entity = new SAPInstance();
                entity = model.ToEntity(entity);

                _sapInstanceService.InsertSAPInstance(entity);

                SuccessNotification(_localizationService.GetResource("Plugins.Misc.SAPAdapter.SAPInstance.CreateSuccess"));

                if (continueEditing)
                {
                    return RedirectToAction("Edit", new { codeName = entity.CodeName });
                }
                return RedirectToAction("Index");
            }

            return View("~/Plugins/SDF.Plugin.Misc.SAPAdapter/Views/SAPInstance/Create.cshtml", model);
        }

        public ActionResult Edit(string codeName)
        {
            var sapInstance = _sapInstanceService.GetSAPInstanceByCodeName(codeName);
            if (sapInstance == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                return View("~/Plugins/SDF.Plugin.Misc.SAPAdapter/Views/SAPInstance/Edit.cshtml", sapInstance.ToModel());
            }
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public ActionResult Edit(SAPClientModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                var sapInstance = _sapInstanceService.GetSAPInstanceByCodeName(model.CodeName);
                if (sapInstance == null)
                    return RedirectToAction("Index");

                //update
                model.ToEntity(sapInstance);
                _sapInstanceService.UpdateSAPInstance(sapInstance);

                SuccessNotification(_localizationService.GetResource("Plugins.Misc.SAPAdapter.SAPInstance.EditSuccess"));

                if (continueEditing)
                {
                    return View("~/Plugins/SDF.Plugin.Misc.SAPAdapter/Views/SAPInstance/Edit.cshtml", model);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            return View("~/Plugins/SDF.Plugin.Misc.SAPAdapter/Views/SAPInstance/Edit.cshtml", model);
        }

        public ActionResult Delete(int Id)
        {
            var sapInstance = _sapInstanceService.GetSAPInstanceById(Id);

            if (sapInstance == null)
            {
                SuccessNotification(_localizationService.GetResource("Plugins.Misc.SAPAdapter.SAPInstance.NotFound"));
                return RedirectToAction("Index");
            }
            else
            {
                _sapInstanceService.DeleteSAPInstance(sapInstance);
                SuccessNotification(_localizationService.GetResource("Plugins.Misc.SAPAdapter.SAPInstance.DeleteSuccess"));
                return RedirectToAction("Index");
            }
        }
    }
}